<?php

//各类结算功能控制器
//文件：application\controllers\advice.php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Statistics extends CI_Controller {

	public $data;
	public $error;

	public function __construct()
	{
		parent::__construct();
		//载入模型
		$this->load->model('Login_model');
		$this->load->model('User_model');
		$this->load->model('Inbound_Type_model');
		$this->load->model('Outbound_Type_model');
		//载入类库
		$this->load->library('Menu_control');
		$this->load->library('Forbid_resubmit');
		//载入配置文件 并 初始化控制器
		$this->data['site'] = $this->config->item('site');
		$this->data['title'] = '各项结算';
		$this->data['styles'] = array('master.css', 'error.css', 'advice.css');
		$this->data['scripts'] = array('master.js', 'advice.js');
		//检查是否是登录用户
		if (!$this->Login_model->check(TRUE))
		{
			header("Location: ".$this->data['site']['root'].'/login');
		}
		//设置站点基本参数
		$this->data['cur_user'] = $this->User_model->getUser($this->input->cookie('user_id'));
		$this->data['header']['username'] = $this->data['cur_user']['realname'];
		$this->data['site']['environment'] = $this->User_model->get_envrionment();
		$this->data['menus'] = $this->config->item('menus');
		$this->data['menus'] = $this->menu_control->main($this->data['menus'], 6);
		$this->data['sub_menus'] = $this->_menus();
		$this->data['sub_menu_index'] = '-1';
	}

	//Privete function in here
	private function _menus()
	{
		return array(
			array(
				'index'	=>	1,
				'url'	=>	$this->data['site']['root'] . 'statistics/sale',
				'name'	=>	'销售统计',
				'right'	=> 	'statistics.stock'
				),
			array(
				'index'	=>	2,
				'url'	=>	$this->data['site']['root'] . 'statistics/sale_return',
				'name'	=>	'销售退货统计',
				'right'	=> 	'statistics.sale_return'
				),
			array(
				'index'	=>	3,
				'url'	=>	$this->data['site']['root'] . 'statistics/order_return',
				'name'	=>	'厂家返修统计',
				'right'	=> 	'statistics.order_return'
				),
		);
	}

	private function _set_menus($index, $title)
	{
		$this->data['sub_menu_index'] = $index;
		$this->data['main']['title'] = $title;
	}

	//默认页面
	public function index()
	{
		$this->sale();
	}

	//销售统计
	public function sale()
	{
		//初始化模型
		$this->Inbound_Type_model->init();
		$this->Outbound_Type_model->init();

		if ($this->check_form())
		{
			//获得销售类型ID


			//获得指定日期所有销售库存单ID

		}

		$this->_set_menus(1, '销售统计');
		$this->load->view_('statistics_view', $this->data);
	}

	//销售退货统计
	public function sale_return()
	{
		//初始化模型
		$this->Inbound_Type_model->init();
		$this->Outbound_Type_model->init();
		

		$this->_set_menus(2, '销售退货统计');
		$this->load->view_('statistics_view', $this->data);
	}

	//厂家返修统计
	public function order_return()
	{
		//初始化模型
		$this->Inbound_Type_model->init();
		$this->Outbound_Type_model->init();
		

		$this->_set_menus(3, '厂家返修统计');
		$this->load->view_('statistics_view', $this->data);
	}

	private function check_form()
	{
		//载入帮助函数
		$this->load->helper('check_date_helper');
		//处理用户输入数据
		if ($this->input->post('submit'))
		{
			//获得输入数据
			$start_date = trim($this->input->post('start_date'));
			$end_date = trim($this->input->post('end_date'));
			$number = trim($this->input->post('goods_number'));

			if (!($start_date && $end_date)
			{
				$this->data['error'] = '日期不能为空';
				return FALSE;
			}
				
			if ($start_date_mktime = check_date($start_date, TRUE) && $end_date_mktime = check_date($end_date, TRUE))
			{
				$this->data['error'] = '日期格式不正确';
				return FALSE;
			}

			if ($start_date_mktime <= $end_date_mktime)
			{
				$this->data['error'] = '开始日期要小于等于结束日期';
				return FALSE;
			}

			return TRUE;

		}
		return FALSE;
	}

}